package com.cargohu.aishouyu.member.service.impl;

import com.cargohu.aishouyu.framework.exception.BusinessException;
import com.cargohu.aishouyu.framework.page.OrderByItem;
import com.cargohu.aishouyu.framework.page.Paging;
import com.cargohu.aishouyu.framework.service.impl.BaseServiceImpl;
import com.cargohu.aishouyu.member.dto.ChatPhraseLabelAddDTO;
import com.cargohu.aishouyu.member.dto.ChatPhraseLabelUpdateDTO;
import com.cargohu.aishouyu.member.entity.ChatPhraseLabel;
import com.cargohu.aishouyu.member.mapper.ChatPhraseLabelMapper;
import com.cargohu.aishouyu.member.req.ChatPhraseLabelRequest;
import com.cargohu.aishouyu.member.resp.ChatPhraseLabelDetailVo;
import com.cargohu.aishouyu.member.resp.ChatPhraseLabelResponse;
import com.cargohu.aishouyu.member.service.ChatPhraseLabelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 聊天常用语标签 服务实现类
 *
 * @author yww
 * @since 2023-11-28
 */
@Slf4j
@Service
public class ChatPhraseLabelServiceImpl extends BaseServiceImpl<ChatPhraseLabelMapper, ChatPhraseLabel> implements ChatPhraseLabelService {

    @Autowired
    private ChatPhraseLabelMapper chatPhraseLabelMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addChatPhraseLabel(ChatPhraseLabelAddDTO chatPhraseLabelAddDTO) throws Exception {
        ChatPhraseLabel chatPhraseLabel = new ChatPhraseLabel();
        BeanUtils.copyProperties(chatPhraseLabelAddDTO, chatPhraseLabel);
        return save(chatPhraseLabel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChatPhraseLabel(ChatPhraseLabelUpdateDTO chatPhraseLabelUpdateDTO) throws Exception {
        Long labelId = chatPhraseLabelUpdateDTO.getLabelId();
        ChatPhraseLabel chatPhraseLabel = getById(labelId);
        if (chatPhraseLabel == null) {
            throw new BusinessException("聊天常用语标签不存在");
        }
        BeanUtils.copyProperties(chatPhraseLabelUpdateDTO, chatPhraseLabel);
        return updateById(chatPhraseLabel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteChatPhraseLabel(Long labelId) throws Exception {
        return removeById(labelId);
    }

    @Override
    public ChatPhraseLabelDetailVo getChatPhraseLabelById(Long labelId) throws Exception {
        return chatPhraseLabelMapper.getChatPhraseLabelById(labelId);
    }

    @Override
    public Paging<ChatPhraseLabelResponse> getChatPhraseLabelList(ChatPhraseLabelRequest chatPhraseLabelRequest) throws Exception {
        handlePage(chatPhraseLabelRequest, OrderByItem.desc("label_id"));
        List<ChatPhraseLabelResponse> list = chatPhraseLabelMapper.getChatPhraseLabelList(chatPhraseLabelRequest);
        Paging<ChatPhraseLabelResponse> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public List<ChatPhraseLabelDetailVo> getAllChatPhraseLabelList() {
        return chatPhraseLabelMapper.getAllChatPhraseLabelList();
    }

}
